<script>
	//注册全局音频事件
	let audioCtx=uni.getBackgroundAudioManager(); 
	export default {
		globalData: {  
			//正在播放的id
			playingId:0,
			//无版权与VIP非自然结束
			end:0,
		},
		onLaunch: function() {
			console.log('App Launch');
			//监听播放id变化
			this.watchplayingId(this.watchBackplayingId);
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			//为新的播放音乐
			async getMusicSrc(){
			    let {playingId}=this.globalData;
				const result=await this.request({url: this.bsurl+'song/url?id='+playingId});
				if(result.data[0].code==-110){
					audioCtx.title="StartLine音乐"
					audioCtx.src = 'null';
					uni.showToast({
						title: '本歌曲为VIP歌曲',
						duration:3000,
						icon: 'none',
					});
					if(this.globalData.playIdList.length>1){
						setTimeout(v=>{
							this.globalData.end+=1;
						},2500) 
					}
				}
				else if(result.data[0].code==404){
					audioCtx.title="StartLine音乐"
					audioCtx.src = 'null';
					uni.showToast({
						title: '亲爱的，暂无版权',
						icon: 'none',
					});
					setTimeout(v=>{
						this.globalData.end+=1;
					},2500) 
				}
				else{
					audioCtx.src =result.data[0].url;
					audioCtx.coverImgUrl=this.$store.state.musicInfo.playingPic;
					audioCtx.singer =this.$store.state.musicInfo.playingAuthor;
					audioCtx.title=this.$store.state.musicInfo.playingName;
				} 
			},
			//监听播放id变化
			watchplayingId:function(method){
				var obj = this.globalData;
				Object.defineProperty(obj,"playingId", {
					configurable: true,
					enumerable: true,
					set: function (value) {
						this._playingId = value;
						method(value);
					},
					get:function(){
						return this._playingId
					}
				})
			},
			//监听播放id变化
			watchBackplayingId(value){
				this.getMusicSrc();
				this.$store.commit('setPlayingId', value)
			},
			//监听非自然结束
			watchend:function(method){
				var obj = this.globalData;
				Object.defineProperty(obj,"end", {
				  configurable: true,
				  enumerable: true,
				  set: function (value) {
					this._end = value;
					method(value);
				  },
				  get:function(){
					return this._end
				  }
				})
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	/*引入官方库*/
	@import './common/uni.css';
	/*引入css动画库*/
	@import '/common/animate.css';
	/*引入公共样式*/
	@import '/common/common.css';
	
	@import "/common/icon.css";
	
	view,text,swiper-item,navigator{
	    padding: 0;
	    margin: 0;
	    box-sizing: border-box;
	    /*图片高度自适应*/
	    height: auto;
	}
	image{
		height: auto;
	}
</style>
